//5
//5 15 40 30 10
// 生成哈夫曼树, 并中序遍历输出,  同样的权值, 右子树更高
// 使用小顶堆优化
const readline = require("readline");
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

class minitopHeap {
    constructor() {
        this.heap = []
    }

    add(treeNode) {
        let heap = this.heap
        heap.push(treeNode)
        if (heap.length === 1) return
        this.upBubble()
    }

    pickupTop() {
        let heap = this.heap
        if (heap.length === 0) return null
        if (heap.length === 1) return heap.shift()
        let temp = heap[0]
        heap[0] = heap.pop()
        if (heap.length > 1) this.downSink()
        return temp

    }

    upBubble() {
        let heap = this.heap
        let index = heap.length - 1
        let parent = Math.floor((index - 1) / 2)
        while (parent >= 0) {
            if (heap[index].value < heap[parent].value) {
                [heap[index], heap[parent]] = [heap[parent], heap[index]]
                index = parent
                parent = Math.floor((index - 1) / 2)
            } else {
                break
            }
        }
    }

    downSink() {
        let heap = this.heap
        let index = 0
        while (true) {
            let left = 2 * index + 1
            let right = 2 * index + 2
            let min = index
            if (left < heap.length && heap[left].value < heap[min].value) {
                min = left
            }
            if (right < heap.length && heap[right].value < heap[min].value) {
                min = right
            }
            if (min !== index) {
                [heap[index], heap[min]] = [heap[min], heap[index]]
                index = min
            } else {
                break
            }
        }
    }
}


class TreeNode {
    constructor(value, left = null, right = null, height = 0) {
        this.value = value
        this.left = left
        this.right = right
        this.height = 0
    }
}

let n;
let initArr = [];
rl.on("line", (line) => {
    if (n === undefined) {
        n = Number(line.trim())
    } else {
        initArr = line.trim().split(/\s+/).map(Number)
        console.log(getResult(n, initArr))
        // clear
        n = undefined
        initArr.length = 0
    }

})

function getResult(n, oldInitArr) {
    let initArr = new minitopHeap()
    for (const num of oldInitArr) {
        initArr.add(new TreeNode(num))
    }

    if (initArr.heap.length === 1) return initArr.pickupTop().value // 一个元素

    while (initArr.heap.length > 1) {
        let left = initArr.pickupTop()
        let right = initArr.pickupTop()
        let root = new TreeNode(left.value + right.value, left, right, Math.max(left.height, right.height) + 1)
        if (left.value === right.value && left.height > right.height) {
            root.left = right
            root.right = left
        }
        initArr.add(root)
    }

    // 中序遍历
    let ans = []
    midTraversal(initArr.pickupTop(), ans)
    return ans.join(" ")
}

function midTraversal(root, ans) {
    if (root.left) {
        midTraversal(root.left, ans)
    }
    ans.push(root.value)
    if (root.right) {
        midTraversal(root.right, ans)
    }
}
